package com.xywl.escort.adapter;

import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.PurchaseOrderDetailBean;
import com.xywl.escort.bean.PurchaseOrderListResp;
import com.xywl.escort.utils.NumberFormatUtils;
import com.xywl.escort.widget.HorizontalDividerItemDecoration;

import java.util.List;

/**
 * Created by 32422 on 2018/5/4.
 */

public class PurchaseOrderAdapter extends BaseQuickAdapter<PurchaseOrderDetailBean, BaseViewHolder> {
    public PurchaseOrderAdapter(int layoutResId, @Nullable List<PurchaseOrderDetailBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final PurchaseOrderDetailBean item) {
        List<PurchaseOrderDetailBean.OrderItemsBean> list = item.orderItems;
        PurchaseOrderListAdapter adapter = new PurchaseOrderListAdapter(R.layout.item_order_purchase_list, list);
        RecyclerView recyclerView = helper.getView(R.id.recycler_purchase_list);

        LinearLayout layoutOperate = helper.getView(R.id.layout_operate);
        Button btnOrderPayment = helper.getView(R.id.btn_order_payment);
        Button btnCancelOrder = helper.getView(R.id.btn_cancel_order);
        Button btnOrderRemind = helper.getView(R.id.btn_order_remind);
        Button btnOrderConfirmReceipt = helper.getView(R.id.btn_order_confirm_receipt);
        Button btnOrderLogistics = helper.getView(R.id.btn_order_logistics);
        Button btnOrderDelete = helper.getView(R.id.btn_order_delete);
        Button btnBuyAgain = helper.getView(R.id.btn_buy_again);
        Button btnOrderComment = helper.getView(R.id.btn_order_comment);
        helper.setText(R.id.text_order_number, item.orderNo);
        // 状态 -1：自动取消 0：未付款1：手动取消 2：待发货 3：已发货 4：已完成 5：退货申请 6：退货中 7：已退货
        switch (item.orderStatus) {
            case 0:
                helper.setText(R.id.text_status, "待付款");
                layoutOperate.setVisibility(View.VISIBLE);
                btnOrderPayment.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.VISIBLE);
                btnOrderRemind.setVisibility(View.GONE);
                btnOrderConfirmReceipt.setVisibility(View.GONE);
                btnOrderLogistics.setVisibility(View.GONE);
                btnOrderDelete.setVisibility(View.GONE);
                btnBuyAgain.setVisibility(View.GONE);
                btnOrderComment.setVisibility(View.GONE);
                break;
            case -1:
            case 1:
                helper.setText(R.id.text_status, "已完成");
                layoutOperate.setVisibility(View.VISIBLE);
                btnOrderPayment.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderRemind.setVisibility(View.GONE);
                btnOrderConfirmReceipt.setVisibility(View.GONE);
                btnOrderLogistics.setVisibility(View.GONE);
                btnOrderDelete.setVisibility(View.VISIBLE);
                btnBuyAgain.setVisibility(View.GONE);
                btnOrderComment.setVisibility(View.GONE);
                break;
            case 2:
                layoutOperate.setVisibility(View.VISIBLE);
                helper.setText(R.id.text_status, "待发货");
                btnOrderPayment.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderRemind.setVisibility(View.VISIBLE);
                btnOrderConfirmReceipt.setVisibility(View.GONE);
                btnOrderLogistics.setVisibility(View.GONE);
                btnOrderDelete.setVisibility(View.GONE);
                btnBuyAgain.setVisibility(View.GONE);
                btnOrderComment.setVisibility(View.GONE);
                break;
            case 3:
                layoutOperate.setVisibility(View.VISIBLE);
                helper.setText(R.id.text_status, "待收货");
                btnOrderPayment.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderRemind.setVisibility(View.GONE);
                btnOrderConfirmReceipt.setVisibility(View.VISIBLE);
                btnOrderLogistics.setVisibility(View.VISIBLE);
                btnOrderDelete.setVisibility(View.GONE);
                btnBuyAgain.setVisibility(View.GONE);
                btnOrderComment.setVisibility(View.GONE);
                break;
            case 4:
                layoutOperate.setVisibility(View.VISIBLE);
                helper.setText(R.id.text_status, "已完成");
                btnOrderPayment.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnOrderRemind.setVisibility(View.GONE);
                btnOrderConfirmReceipt.setVisibility(View.GONE);
                btnOrderLogistics.setVisibility(View.VISIBLE);
                btnOrderDelete.setVisibility(View.VISIBLE);
                btnBuyAgain.setVisibility(View.VISIBLE);
                btnOrderComment.setVisibility(View.VISIBLE);
                break;
            case 5:
                helper.setText(R.id.text_status, "退货申请");
                layoutOperate.setVisibility(View.GONE);
                break;
            case 6:
                helper.setText(R.id.text_status, "退货中");
                layoutOperate.setVisibility(View.GONE);
                break;
            case 7:
                helper.setText(R.id.text_status, "已退货");
                layoutOperate.setVisibility(View.GONE);
                break;
            default:
                helper.setText(R.id.text_status, "");
                layoutOperate.setVisibility(View.GONE);
                break;
        }
        if (null != item.orderAmountTotal) {
            String shopInfo = "共" + item.productCount + "件商品" + " 合计 ￥" + NumberFormatUtils.getInstance().format2Decimal(item.orderAmountTotal.doubleValue()) + "元 (含运费0.00)";
            helper.setText(R.id.layout_shop_info, shopInfo);
        }
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        Paint paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setColor(ContextCompat.getColor(mContext, R.color.line_color));
        paint.setAntiAlias(true);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).paint(paint).build());
        recyclerView.setLayoutManager(manager);

        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance()
                        .build("/xy/order/purchase/detail")
                        .withString("orderId", item.id)
                        .navigation();
                // ToastUtil.showTip(mContext, "RecycleItemProvider position=" + position);
            }
        });

        helper.addOnClickListener(R.id.btn_order_payment);
        helper.addOnClickListener(R.id.btn_cancel_order);
        helper.addOnClickListener(R.id.btn_order_remind);
        helper.addOnClickListener(R.id.btn_order_confirm_receipt);
        helper.addOnClickListener(R.id.btn_order_logistics);
        helper.addOnClickListener(R.id.btn_order_delete);
        helper.addOnClickListener(R.id.btn_buy_again);
        helper.addOnClickListener(R.id.btn_order_comment);
    }
}
